package com.blog.backend.controller;

import com.blog.backend.annotation.LoginAnno;
import com.blog.backend.domain.BlogSummary;
import com.blog.backend.req.SettingReq;
import com.blog.backend.req.UserReq;
import com.blog.backend.resp.SummaryResp;
import com.blog.backend.resp.SysSettingResp;
import com.blog.backend.resp.TokenResult;
import com.blog.backend.service.BlogIndexManagerService;
import com.blog.backend.service.UserManagerService;
import com.blog.backend.utils.Result;
import com.blog.backend.vo.PageRequest;
import com.blog.backend.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@RestController()
@Api(tags = {"用户账号管理接口"})
public class UserManageController {

//    @Resource
//    private UserManagerService userManagerService;
    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private BlogIndexManagerService blogIndexManagerService;

    @PostMapping("/user/login")
    @ResponseBody
    @ApiOperation("管理系统登录接口")
    public Result login(@RequestBody @Valid UserReq req){
        //登录
        UserManagerService userManagerService = this.getUserManagerService();
        TokenResult result=userManagerService.login(req);
        return Result.success(result);
    }

    @PostMapping("/user/createAccount")
    @ResponseBody
    public Result createAccount(@RequestBody UserReq req){
        UserManagerService userManagerService = this.getUserManagerService();
        String result=userManagerService.createAccount(req);
        return Result.success(result);
    }

    //根据上下文applicationContext获取bean对象
    public UserManagerService getUserManagerService(){
        Map<String, UserManagerService> beansOfType = applicationContext.getBeansOfType(UserManagerService.class);
        return  beansOfType.get("userManagerServiceImpl");
    }


    //获取系统设置列表
    @PostMapping("/blog/setting/getSettingList")
    @LoginAnno
    @ApiOperation("获取系统设置列表")
    public Result<PageResult<SysSettingResp>> getSettingList(@RequestBody PageRequest request){
        PageResult<SysSettingResp> pageResult=  blogIndexManagerService.getSettingList(request);
        return Result.success(pageResult);
    }

    @GetMapping("/blog/setting/getSettingDetail")
    @LoginAnno
    @ApiOperation("获取配置详情")
    public Result<SysSettingResp> getSettingDetail(@RequestParam String settingId){
        SysSettingResp resp=blogIndexManagerService.getSettingDetail(settingId);
        return Result.success(resp);
    }

    @PostMapping("/blog/setting/updateSettingDetail")
    @LoginAnno
    @ApiOperation("修改配置")
    public Result<Boolean> updateSettingDetail(@RequestBody SettingReq req){
        Boolean result=blogIndexManagerService.updateSettingDetail(req);
        return Result.success(result);
    }

    @PostMapping("/blog/summary/getSummaryList")
    @LoginAnno
    @ApiOperation("年度总结列表")
    public Result<PageResult<SummaryResp>> getSummaryList(@RequestBody PageRequest request){
        PageResult<SummaryResp> respPageResult=blogIndexManagerService.getSummaryList(request);
        return Result.success(respPageResult);
    }

    @PostMapping("/blog/summary/addSummary")
    @LoginAnno
    @ApiOperation("新增年度总结")
    public Result<Boolean> addSummary(@RequestBody BlogSummary req){
        Boolean result = blogIndexManagerService.addSummary(req);
        return Result.success(result);
    }

    @GetMapping("/blog/summary/getSummaryDetail")
    @LoginAnno
    @ApiOperation("查看总结详情")
    public Result<SummaryResp> getSummaryDetail(@RequestParam String summaryId){
        SummaryResp resp=blogIndexManagerService.getSummaryDetail(summaryId);
        return Result.success(resp);
    }

    @PostMapping("/blog/summary/updateSummaryDetail")
    @LoginAnno
    @ApiOperation("更新总结详情")
    public Result<Boolean> updateSummaryDetail(@RequestBody BlogSummary req){
        Boolean result= blogIndexManagerService.updateSummaryDetail(req);
        return Result.success(result);
    }

    @GetMapping("/blog/summary/deleteSummary")
    @LoginAnno
    @ApiOperation("删除总结")
    public Result<Boolean> deleteSummary(@RequestParam String summaryId){
        Boolean result=blogIndexManagerService.deleteSummary(summaryId);
        return Result.success(result);
    }


    @GetMapping("/user/layout")
    @LoginAnno
    public Result<Boolean> layout(HttpServletRequest request){
        UserManagerService userManagerService = this.getUserManagerService();
      Boolean result=  userManagerService.layout(request);
      return Result.success(result);
    }
}
